﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TravelWeb.Models;

namespace TravelWeb.Controllers
{
    public class TourController : Controller
    {
        private TravelWebDB db = new TravelWebDB();

        //
        // GET: /Tour/

        public ViewResult Index()
        {
            var tours = db.Tours.Include(t => t.Promotion);
            return View(tours.ToList());
        }

        //
        // GET: /Tour/Details/5

        public ViewResult Details(int id)
        {
            Tour tour = db.Tours.Find(id);
            return View(tour);
        }

        //
        // GET: /Tour/Create

        public ActionResult Create()
        {
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName");
            return View();
        }

        //
        // POST: /Tour/Create

        [HttpPost]
        public ActionResult Create(Tour tour)
        {
            if (ModelState.IsValid)
            {
                tour.Status = "New";
                db.Tours.Add(tour);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", tour.PromotionID);
            return View(tour);
        }

        //
        // GET: /Tour/Edit/5

        public ActionResult Edit(int id)
        {
            Tour tour = db.Tours.Find(id);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", tour.PromotionID);
            return View(tour);
        }

        //
        // POST: /Tour/Edit/5

        [HttpPost]
        public ActionResult Edit(Tour tour)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tour).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", tour.PromotionID);
            return View(tour);
        }

        //
        // GET: /Tour/Delete/5

        public ActionResult Delete(int id)
        {
            Tour tour = db.Tours.Find(id);
            return View(tour);
        }

        //
        // POST: /Tour/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Tour tour = db.Tours.Find(id);
            db.Tours.Remove(tour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Approve(int id)
        {
            Tour tour = db.Tours.Find(id);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", tour.PromotionID);
            tour.Status = "Open";
            db.SaveChanges();
            return View(tour);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}